package vehement.game.graphics.primitive;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

//import javax.microedition.khronos.opengles.GL10;

import vehement.game.interfaces.Drawable;


/**
 * This will draw a square
 * @author bloodred
 *
 */
public class GLSquare implements Drawable
{
	// globals
	private IntBuffer mVertexBuffer;   // floor vertices
	private IntBuffer mColorBuffer;
	private ByteBuffer mIndexBuffer;     // tex coords

	/** create the vertices and tex coords buffers for the floor
	*   of size-by-size
	*/
	public GLSquare(){
		int size = 1;
        int one = 0x10000;
        int vertices[] = {
                -one, -one, 0,
                one, -one,  0,
                one,  one,  0,
                -one,  one, 0,
                
        };

        int colors[] = {
                0,    0,    0,  one,
                one,    0,    0,  one,
                one,  one,    0,  one,
                0,  one,    0,  one,
        };

        byte indices[] = {
                0, 3, 2,    0,2,1
        };

        // Buffers to be passed to gl*Pointer() functions
        // must be direct, i.e., they must be placed on the
        // native heap where the garbage collector cannot
        // move them.
        //
        // Buffers with multi-byte datatypes (e.g., short, int, float)
        // must have their byte order set to native order

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asIntBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);

        ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
        cbb.order(ByteOrder.nativeOrder());
        mColorBuffer = cbb.asIntBuffer();
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
        mIndexBuffer.put(indices);
        mIndexBuffer.position(0);
		/*
		// create vertices
		byte pt = (byte)(size/2);
		byte npt = (byte) (-size/2);   // negative pt
		byte[] verts = { pt,pt,0, npt,pt,0, npt,npt,0, pt,npt,0};
		vertsBuf = ByteBuffer.allocateDirect(verts.length);
		vertsBuf.put(verts).rewind();
		// create texture coordinates
		byte[] tcs = {0,0, 1,0, 0,1, 1,1};
		tcsBuf = ByteBuffer.allocateDirect(tcs.length);
		tcsBuf.put(tcs).rewind();
		*/
	}

	public void draw() {
		/*
		// TODO Auto-generated method stub
        gl.glFrontFace(GL10.GL_CW);
		//gl.glDisable(GL10.GL_CULL_FACE);
        gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
        gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);	
	*/
	}

}
